package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.domain.entity.Product;
import org.example.mapper.ProductMapper;
import org.example.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;

    //获取产品及评论,图片等所有信息
    @Override
    public List<Product> getProducts() {
        return productMapper.selectProducts();
    }

    //仅获取分页的产品信息
    @Override
    public Map<String,Object> getByPage(Integer page, Integer limit) {
        Map<String,Object> result = new HashMap<>();
        Page<Product> pageResult = PageHelper.startPage(page, limit);
        productMapper.selectList(null);
        List<Product> products = pageResult.getResult();
        result.put("products",products);
        result.put("total",pageResult.getTotal());
        return result;
    }

    @Override
    public Boolean updateProduct(Product product) {
        return productMapper.updateById(product)>0;
    }

    @Override
    public Boolean deleteProduct(Integer productId) {
        return productMapper.deleteById(productId)>0;
    }

    @Override
    public Boolean insertProduct(Product product) {
        return productMapper.insert(product)>0;
    }

    @Override
    public List<Product> getProductsById(Integer productId) {
        List<Product> products = new ArrayList<>();
        Product product = productMapper.selectById(productId);
        products.add(product);
        return products;
    }

    @Override
    public Map<String,Object> getProductsByName(String productName, Integer page, Integer limit) {
        Map<String,Object> result = new HashMap<>();
        Page<Product> pageResult = PageHelper.startPage(page, limit);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_name",productName);
        productMapper.selectList(queryWrapper);
        List<Product> products = pageResult.getResult();
        result.put("products",products);
        result.put("total",pageResult.getTotal());
        return result;
    }

    @Override
    public Map<String, Object> getProductsByCreateTime(Date[] createTime, Integer page, Integer limit) {
        Map<String,Object> result = new HashMap<>();
        Page<Product> pageResult = PageHelper.startPage(page, limit);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time",createTime[0]).and(productQueryWrapper -> productQueryWrapper.le("create_time",createTime[1]));
        productMapper.selectList(queryWrapper);
        List<Product> products = pageResult.getResult();
        result.put("products",products);
        result.put("total",pageResult.getTotal());
        return result;
    }

    @Override
    public Map<String, Object> getProductsByUpdateTime(Date[] updateTime, Integer page, Integer limit) {
        Map<String,Object> result = new HashMap<>();
        Page<Product> pageResult = PageHelper.startPage(page, limit);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("update_time",updateTime[0]).le("update_time",updateTime[1]);
        productMapper.selectList(queryWrapper);
        List<Product> products = pageResult.getResult();
        result.put("products",products);
        result.put("total",pageResult.getTotal());
        return result;
    }


}



